package com.fosung.frame.tsdemo.front.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fosung.frame.common.mapper.JsonMapper;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.cms.entity.Category;
import com.fosung.frame.modules.sys.entity.User;
import com.fosung.frame.modules.sys.service.DictService;
import com.fosung.frame.modules.sys.service.SystemService;
import com.fosung.frame.tsdemo.entity.meeting.Meeting;
import com.fosung.frame.tsdemo.entity.module.Module;
import com.fosung.frame.tsdemo.entity.notice.Notice;
import com.fosung.frame.tsdemo.entity.rulemodule.RuleModule;
import com.fosung.frame.tsdemo.front.service.AppService;
@Controller
@RequestMapping(value = "/app")
public class AppController extends BaseController {

	@Autowired
	private AppService service;
	@Autowired
	private DictService dictService;
	
	
	public String getIp(){
		return dictService.get("d99234504b8e47788280caa36831b770").getValue();
	}
	
	
	@ResponseBody
	@RequestMapping("login")
	public String Login(HttpServletResponse response,String loginName,String password,HttpServletRequest  request){
		response.setHeader("Access-Control-Allow-Origin", "*");
		password=SystemService.entryptPassword(password);
		User user=service.getUserByName(loginName);
		Map<String,Object> result=new HashMap<String,Object>();
		if(user !=null){//用户不为空，获取用户的权限
			result.put("code", true);
			result.put("user", user);
		}else{	//返回用户名密码错误
			result.put("msg", "用户名或密码错误");
			result.put("code", false);
		}
		return JsonMapper.toJsonString(result);
	}
	/**
	 * 查询出底部导航栏的列表--tsbottom
	 * @return
	 */
	@ResponseBody
	@RequestMapping("bottom")
	public String bottom(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<String,Object>();
		List<Category> list=service.findCategoryList("tsbottom");
		result.put("list", list);
		return JsonMapper.toJsonString(result);
	}
	/**
	 * 进入首页，查询出菜单的列表--tsmenu，
	 * @return
	 */
	@ResponseBody
	@RequestMapping("main")
	public String main(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<String,Object>();
		List<Category> list=service.findCategoryList("tsmenu");
		result.put("list", list);
		return JsonMapper.toJsonString(result);
	}
	
	/**
	 * 点击顶部菜单，查询出它的下级菜单和banner页的列表
	 *  根据下级菜单第一个的ID获取到List信息
	 *  根据获取到的栏目信息，判断是否是首页，
	 *  										  是  添加模块的列表
	 *  										  不是 不添加模块的列表
	 * @param id 菜单ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getNoticeByCategoryId")
	public String getNoticeByCategoryId(HttpServletResponse response,String userId,String categoryId,boolean flag){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<String,Object>();
		List<Category> listCategory=service.queryCategoryList(categoryId);
		List<Notice> listbanner=service.findNoticeList(categoryId, "1", userId);//banner页的列表
		for(int i=0;i<listbanner.size();i++){
			Notice map = listbanner.get(i);
			String img = map.getImage();
			if(!StringUtils.isEmpty(img)){
				map.setImage(getIp() + img.substring(img.indexOf("/userfiles")));
			}
		}
		if(flag){//是首页
			List<Module> listModule=service.queryModuleList(userId);
			for(int i=0;i<listModule.size();i++){
				Module map = listModule.get(i);
				String img = map.getImg();
				if(!StringUtils.isEmpty(img)){
					map.setImg(getIp() + img.substring(img.indexOf("/userfiles")));
				}
			}
			result.put("listModule", listModule);
			if(listCategory.size()>0){
				List<Meeting> listMeeting=service.findMeetingList(listCategory.get(0).getId(), userId);
				for(int i=0;i<listMeeting.size();i++){
					Meeting map = listMeeting.get(i);
					String img = map.getImage();
					if(!StringUtils.isEmpty(img)){
						map.setImage(getIp() + img.substring(img.indexOf("/userfiles")));
					}
					map.setContent(map.getContent().substring(0,map.getContent().indexOf("<img")) +
							map.getContent().substring(map.getContent().lastIndexOf("/>")+2));
				}
				result.put("listMeeting", listMeeting);
			}
			
		}else{
			if(listCategory.size()>0){
				List<Notice> listNotice=service.findNoticeList(listCategory.get(0).getId(), "2", userId);//2级列表
				for(int i=0;i<listNotice.size();i++){
					Notice map = listNotice.get(i);
					String img = map.getImage();
					if(!StringUtils.isEmpty(img)){
						map.setImage(getIp() + img.substring(img.indexOf("/userfiles")));
					}
					if(map.getContent().indexOf("<img")!=-1){
						map.setContent(map.getContent().substring(0,map.getContent().indexOf("<img")) +
								map.getContent().substring(map.getContent().lastIndexOf("/>")+2));
					}
					
				}
				result.put("listNotice", listNotice);
			}
		}
		
		result.put("listCategory", listCategory);
		result.put("listbanner", listbanner);
		
		return JsonMapper.toJsonString(result);
	}
	
	@ResponseBody
	@RequestMapping("getNoticeList")
	public String getNoticeList(HttpServletResponse response,String userId,String categoryId,boolean flag){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<String,Object>();
//		RuleModule ruleModule=service.getRuleModuleByUserId(userId);
		if(flag){//是首页
			List<Meeting> listMeeting=service.findMeetingList(categoryId, userId);
			for(int i=0;i<listMeeting.size();i++){
				Meeting map = listMeeting.get(i);
				String img = map.getImage();
				if(!StringUtils.isEmpty(img)){
					map.setImage(getIp() + img.substring(img.indexOf("/userfiles")));
				}
				map.setContent(map.getContent().substring(0,map.getContent().indexOf("<img")) +
						map.getContent().substring(map.getContent().lastIndexOf("/>")+2));
			}
			result.put("listMeeting", listMeeting);
		}else{
			List<Notice> listNotice=service.findNoticeList(categoryId, "2", userId);//2级列表
			for(int i=0;i<listNotice.size();i++){
				Notice map = listNotice.get(i);
				String img = map.getImage();
				if(!StringUtils.isEmpty(img)){
					map.setImage(getIp() + img.substring(img.indexOf("/userfiles")));
				}
				if(map.getContent().indexOf("<img")!=-1){
					map.setContent(map.getContent().substring(0,map.getContent().indexOf("<img")) +
							map.getContent().substring(map.getContent().lastIndexOf("/>")+2));
				}
			}
			result.put("listNotice", listNotice);
		}
		return JsonMapper.toJsonString(result);
	}
}
